package com.zhongxiangspace.controller.pagemanage;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.House;
import com.zhongxiangspace.entity.OfficeBuildings;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Response;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.service.HouseService;
import com.zhongxiangspace.service.OfficeBuildingService;
import com.zhongxiangspace.tools.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 房源
 */
@Controller
public class OfficeHouseController {
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private HouseService houseService;

    @GetMapping("/officeBuilding/house/add.do")
    public String add(HttpServletRequest request, Model model) {
        //获取区域
        List<County> tradeAreaList = countyService.queryAllType(0);
        model.addAttribute("data", tradeAreaList);
        return "basics/house/house_add";
    }


    /**
     * 编辑
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/officeBuilding/house/edit.do")
    public String edit(HttpServletRequest request, Model model, Integer id, String type) {
        List<County> countyList = countyService.queryAllType(0);
        model.addAttribute("data", countyList);
        House house = this.houseService.queryOne(id);
        model.addAttribute("object", house);
        String gson = new Gson().toJson(house);
        model.addAttribute("houseJson", gson);
        //获取区域下所有商圈
        List<County> tradeAreaList = countyService.queryAllType(house.getCounty_id());
        model.addAttribute("tradeAreaList", tradeAreaList);
        //获取商圈下所有楼盘
        QueryAnalyzer queryAnalyzer = new QueryAnalyzer();
        queryAnalyzer.setTrade_area_id(house.getTrade_area_id());
        List<OfficeBuildings> officeBuildingsList = this.officeBuildingService.queryByQueryAnalyzer(queryAnalyzer);
        model.addAttribute("officeBuildingsList", officeBuildingsList);
        OfficeBuildings officeBuildings=this.officeBuildingService.queryOne(house.getBuilding_id());
        model.addAttribute("officeBuildingName",officeBuildings.getBuilding_name());
        if (type!=null && !type.isEmpty() && type.equals("SEE")) {
            return "basics/house/house_see";
        } else {
            return "basics/house/house_updata";
        }
    }


    /**
     * 保存 updata
     */
    @ResponseBody
    @PostMapping("/officeBuilding/house/update.do")
    public Result update(House house) {
        try {
            house.setModify_time(DateUtil.getDateTime());
            boolean flag = this.houseService.updata(house);
            return new NormalResult("SUCCESS");
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }

    /**
     * 异步热门和非热门
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/house/statusTop.do")
    public Response statusTop(Integer id) {
        if (id != null) {
            House house = this.houseService.queryOne(id);
            house.setModify_time(DateUtil.getDateTime());
            if (house != null && house.getTopCode().equals(0)) {
                house.setTopCode(1);
                boolean flag = this.houseService.updata(house);
            } else if (house != null && house.getTopCode().equals(1)) {
                house.setTopCode(0);
                boolean flag = this.houseService.updata(house);
            }
        }
        Response result = new Response(0, "success", 0, null);
        return result;
    }

    /**
     * 异步修改上下线 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/house/status.do")
    public Response status(Integer id) {
        if (id != null) {
            House house = this.houseService.queryOne(id);
            house.setModify_time(DateUtil.getDateTime());
            if (house != null && house.getShowCode().equals(0)) {
                house.setShowCode(1);
                boolean flag = this.houseService.updata(house);
            } else if (house != null && house.getShowCode().equals(1)) {
                house.setShowCode(0);
                boolean flag = this.houseService.updata(house);
            }
        }
        Response result = new Response(0, "success", 0, null);
        return result;
    }

    /**
     * 异步删除 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/house/delete.do")
    public Response delete(Integer id) {
        if (id != null) {
            boolean flag = this.houseService.delete(id);
        }
        Response result = new Response(0, "success", 0, null);
        return result;
    }


    /**
     * 房源list
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/officeBuilding/house/list.do")
    public String list(HttpServletRequest request, Model model) {
        List<County> tradeAreaList = countyService.queryAllType(null);
        model.addAttribute("data", tradeAreaList);
        String gson = new Gson().toJson(tradeAreaList);
        model.addAttribute("dataJson", gson);
        return "basics/house/house_list";
    }

    /**
     * 异步
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/officeBuilding/house/ajaxList.do")
    public Response ajaxList(HttpServletRequest request, QueryAnalyzer queryAnalyzer) {
        PageHelper.startPage(queryAnalyzer.getPage(), queryAnalyzer.getLimit(), "modify_time DESC  ");
        Page<Map<String, Object>> houseList = (Page<Map<String, Object>>) this.houseService.queryAll(queryAnalyzer);
        try {
            return new Response(0, "222", houseList.getTotal(), houseList.getResult());
        } catch (Exception e) {
            return new Response(1200, "ERROR", 2555, null);
        }
    }

    /**
     * 查询楼盘
     *
     * @param request
     * @param queryAnalyzer
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/house/ajxaBuilding.do")
    public Result ajxaBuilding(HttpServletRequest request, QueryAnalyzer queryAnalyzer) {
        List<OfficeBuildings> officeBuildingsList = this.officeBuildingService.queryByQueryAnalyzer(queryAnalyzer);
        return new NormalResult(officeBuildingsList);
    }


    /**
     * 插入房源
     *
     * @param request
     * @param house
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/house/inster.do")
    public Result inster(HttpServletRequest request, House house) {
        try {
            house.setCreate_time(DateUtil.getDateTime());
            house.setModify_time(DateUtil.getDateTime());
            boolean flag = this.houseService.inster(house);
            return new NormalResult("SUCCESS");
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }

    /**
     * 查询data
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/officeBuilding/house/queryAll.do")
    public Response queryAll(QueryAnalyzer queryAnalyzer) {
        try {
            PageHelper.startPage(queryAnalyzer.getPage(), queryAnalyzer.getLimit());
         /*   List<House> houseList = this.houseService.queryAll(queryAnalyzer);*/
            return new Response(1111, "222", 2555, null);
        } catch (Exception e) {
            return new Response(1000, "error", 0, null);
        }
    }
}
